//
//  DeviceNameLib.m
//  DeviceNameLib
//
//  Created by zhangcong on 2017/10/12.
//  Copyright © 2017年 zhangcong. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DeviceNameLib.h"
#import "GuidTool.h"
#include <sys/types.h>
#include <sys/sysctl.h>

@implementation DeviceNameLib

+ (NSString *)deviceId {
    return [GuidTool Guid];
}

+ (NSString *)serverDeviceId {
    return [GuidTool serverId];
}

+ (void)removeLocalDeviceId {
    [GuidTool deleteGuid];
}

+ (void)removeServerDeviceId {
    [GuidTool deleteServerId];
}

+ (void)saveServerDeviceId:(NSString *)deviceId {
    [GuidTool saveServerGuid:deviceId];
}

+ (NSString *)deviceName {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithUTF8String:machine];
    free(machine);
    
    NSDictionary *dictHwMachine = @{
                                    
                                    //iPad.
                                    @"iPad1,1" : @[ @"iPad 1", @"Wi-Fi / GSM", @"A1219 / A1337" ],
                                    @"iPad2,1" : @[ @"iPad 2", @"Wi-Fi", @"A1395" ],
                                    @"iPad2,2" : @[ @"iPad 2", @"GSM", @"A1396" ],
                                    @"iPad2,3" : @[ @"iPad 2", @"CDMA", @"A1397" ],
                                    @"iPad2,4" : @[ @"iPad 2", @"Wi-Fi Rev A", @"A1395" ],
                                    @"iPad2,5" : @[ @"iPad mini 1", @"Wi-Fi", @"A1432" ],
                                    @"iPad2,6" : @[ @"iPad mini 1", @"GSM", @"A1454" ],
                                    @"iPad2,7" : @[ @"iPad mini 1", @"GSM+CDMA", @"A1455" ],
                                    @"iPad3,1" : @[ @"iPad 3", @"Wi-Fi", @"A1416" ],
                                    @"iPad3,2" : @[ @"iPad 3", @"GSM+CDMA", @"A1403" ],
                                    @"iPad3,3" : @[ @"iPad 3", @"GSM", @"A1430" ],
                                    @"iPad3,4" : @[ @"iPad 4", @"Wi-Fi", @"A1458" ],
                                    @"iPad3,5" : @[ @"iPad 4", @"GSM", @"A1459" ],
                                    @"iPad3,6" : @[ @"iPad 4", @"GSM+CDMA", @"A1460" ],
                                    @"iPad4,1" : @[ @"iPad Air", @"Wi‑Fi", @"A1474" ],
                                    @"iPad4,2" : @[ @"iPad Air", @"Cellular", @"A1475" ],
                                    @"iPad4,3" : @[ @"iPad Air"],
                                    @"iPad4,4" : @[ @"iPad mini 2", @"Wi‑Fi", @"A1489" ],
                                    @"iPad4,5" : @[ @"iPad mini 2", @"Cellular", @"A1517" ],
                                    @"iPad4,6" : @[ @"iPad mini 3"],
                                    @"iPad4,7" : @[ @"iPad mini 3"],
                                    @"iPad4,8" : @[ @"iPad mini 3"],
                                    @"iPad4,9" : @[ @"iPad mini 3"],
                                    @"iPad5,1" : @[ @"iPad mini 4"],
                                    @"iPad5,2" : @[ @"iPad mini 4"],
                                    @"iPad5,3" : @[ @"iPad Air 2"],
                                    @"iPad5,4" : @[ @"iPad Air 2"],
                                    @"iPad6,7" : @[ @"iPad Pro (12.9-inch)"],
                                    @"iPad6,8" : @[ @"iPad Pro (12.9-inch)"],
                                    @"iPad6,3" : @[ @"iPad Pro (9.7-inch)"],
                                    @"iPad6,4" : @[ @"iPad Pro (9.7-inch)"],
                                    @"iPad6,11" : @[ @"iPad (5th generation)"],
                                    @"iPad6,12" : @[ @"iPad (5th generation)"],
                                    @"iPad7,1" : @[ @"iPad Pro (12.9-inch, 2nd generation)"],
                                    @"iPad7,2" : @[ @"iPad Pro (12.9-inch, 2nd generation)"],
                                    @"iPad7,3" : @[ @"iPad Pro (10.5-inch)"],
                                    @"iPad7,4" : @[ @"iPad Pro (10.5-inch)"],
                                    @"iPad8,1" : @[ @"iPad Pro 11-inch"],
                                    @"iPad8,2" : @[ @"iPad Pro 11-inch"],
                                    @"iPad8,3" : @[ @"iPad Pro 11-inch"],
                                    @"iPad8,4" : @[ @"iPad Pro 11-inch"],
                                    @"iPad8,5" : @[ @"iPad Pro 12.9-inch"],
                                    @"iPad8,6" : @[ @"iPad Pro 12.9-inch"],
                                    @"iPad8,7" : @[ @"iPad Pro 12.9-inch"],
                                    @"iPad8,8" : @[ @"iPad Pro 12.9-inch"],
                                    @"iPad11,1" : @[ @"iPad Mini 5"],
                                    @"iPad11,2" : @[ @"iPad Mini 5"],
                                    @"iPad11,3" : @[ @"iPad Air 3"],
                                    @"iPad11,4" : @[ @"iPad Air 3"],
                                    
                                    
                                    //iPhone.
                                    @"iPhone1,1" : @[ @"iPhone 2G", @"GSM", @"A1203" ],
                                    @"iPhone1,2" : @[ @"iPhone 3G", @"GSM", @"A1241 / A13241" ],
                                    @"iPhone2,1" : @[ @"iPhone 3GS", @"GSM", @"A1303 / A13251" ],
                                    @"iPhone3,1" : @[ @"iPhone 4", @"GSM", @"A1332" ],
                                    @"iPhone3,2" : @[ @"iPhone 4", @"GSM Rev A", @"-" ],
                                    @"iPhone3,3" : @[ @"iPhone 4", @"CDMA", @"A1349" ],
                                    @"iPhone4,1" : @[ @"iPhone 4S", @"GSM+CDMA", @"A1387 / A14311" ],
                                    @"iPhone5,1" : @[ @"iPhone 5", @"GSM", @"A1428" ],
                                    @"iPhone5,2" : @[ @"iPhone 5", @"GSM+CDMA", @"A1429 / A14421" ],
                                    @"iPhone5,3" : @[ @"iPhone 5C", @"GSM", @"A1456 / A1532" ],
                                    @"iPhone5,4" : @[ @"iPhone 5C", @"Global", @"A1507 / A1516 / A1526 / A1529" ],
                                    @"iPhone6,1" : @[ @"iPhone 5S", @"GSM", @"A1433 / A1533" ],
                                    @"iPhone6,2" : @[ @"iPhone 5S", @"Global", @"A1457 / A1518 / A1528 / A1530" ],
                                    @"iPhone7,1" : @[ @"iPhone 6 Plus (A1522/A1524)"],
                                    @"iPhone7,2" : @[ @"iPhone 6 (A1549/A1586)" ],
                                    @"iPhone8,1" : @[ @"iPhone 6s " ],
                                    @"iPhone8,2" : @[ @"iPhone 6s Plus" ],
                                    @"iPhone8,4" : @[ @"iPhone SE" ],
                                    @"iPhone9,1" : @[ @"iPhone 7" ],
                                    @"iPhone9,2" : @[ @"iPhone 7 Plus" ],
                                    @"iPhone9,3" : @[ @"iPhone 7" ],
                                    @"iPhone9,4" : @[ @"iPhone 7 Plus" ],
                                    
                                    @"iPhone10,4" : @[ @"iPhone 8" ],
                                    @"iPhone10,1" : @[ @"iPhone 8" ],
                                    @"iPhone10,2" : @[ @"iPhone 8 Plus" ],
                                    @"iPhone10,5" : @[ @"iPhone 8 Plus" ],
                                    @"iPhone10,3" : @[ @"iPhone X" ],
                                    @"iPhone10,6" : @[ @"iPhone X" ],
                                    
                                    @"iPhone11,2":@[@"iPhone XS"],
                                    @"iPhone11,4":@[@"iPhone XS Max"],
                                    @"iPhone11,6":@[@"iPhone XS Max"],
                                    @"iPhone11,8":@[@"iPhone XR"],
                                    
                                    @"iPhone12,1":@[@"iPhone 11"],
                                    @"iPhone12,3":@[@"iPhone 11 Pro"],
                                    @"iPhone12,5":@[@"iPhone 11 Pro Max"],
                                    
                                    //iPod.
                                    @"iPod1,1" : @[ @"iPod touch 1G", @"-", @"A1213" ],
                                    @"iPod2,1" : @[ @"iPod touch 2G", @"-", @"A1288" ],
                                    @"iPod3,1" : @[ @"iPod touch 3G", @"-", @"A1318" ],
                                    @"iPod4,1" : @[ @"iPod touch 4G", @"-", @"A1367" ],
                                    @"iPod5,1" : @[ @"iPod touch 5G", @"-", @"A1421 / A1509" ],
                                    @"iPod7,1" : @[ @"iPod touch 6G", @"-", @"A1367" ],
                                    //
                                    @"i386"    : @[ @"iPhone Simulator" ],
                                    @"x86_64"  : @[@"iPhone Simulator" ]
                                    };
    
    NSArray *arrayHw = [dictHwMachine objectForKey:platform];
    if(arrayHw !=nil)
    {
        return [arrayHw objectAtIndex:0];
    }
    else
    {
        if ([platform hasSuffix:@"86"] || [platform isEqual:@"x86_64"])
        {
            return @"Simulator";
        }else
            return @"Unknown iOS device";
    }
    return nil;
}

+ (NSString *)deviceVersion {
    return [[UIDevice currentDevice] systemVersion];
}

@end
